package juc;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
/*
* 添加操作多时，效率低，因为添加时会进行复制，开销非常大，并发迭代多时可选择。*/
public class TestCopyOnWriteArrayList {
    public static void main(String[] args) {
        HelloThread thread = new HelloThread();
        new Thread(thread).start();

    }
}
class HelloThread implements Runnable {

    //将一个ArrayList转换为线程安全的
    //private List<String> list = Collections.synchronizedList(new ArrayList<String>());

    private static CopyOnWriteArrayList list = new CopyOnWriteArrayList<String>();

    static {
        list.add("AA");
        list.add("BB");
        list.add("CC");

    }

    @Override
    public void run() {
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()){
            System.out.println(iterator.next());
            list.add("AA");
        }

    }
}
